<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateAbnormalCarAPIRequest;
use App\Http\Requests\API\UpdateAbnormalCarAPIRequest;
use App\Models\AbnormalCar;
use App\Repositories\AbnormalCarRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class AbnormalCarController
 * @package App\Http\Controllers\API
 */

class AbnormalCarAPIController extends AppBaseController
{
    /** @var  AbnormalCarRepository */
    private $abnormalCarRepository;

    public function __construct(AbnormalCarRepository $abnormalCarRepo)
    {
        $this->abnormalCarRepository = $abnormalCarRepo;
    }

    /**
     * Display a listing of the AbnormalCar.
     * GET|HEAD /abnormalCars
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->abnormalCarRepository->pushCriteria(new RequestCriteria($request));
        $this->abnormalCarRepository->pushCriteria(new LimitOffsetCriteria($request));
        $abnormalCars = $this->abnormalCarRepository->paginate($request->limit);

        return $this->sendResponse($abnormalCars->toArray(), '成功获取数据');
    }

    /**
     * Store a newly created AbnormalCar in storage.
     * POST /abnormalCars
     *
     * @param CreateAbnormalCarAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateAbnormalCarAPIRequest $request)
    {
        $input = $request->all();

        $abnormalCars = $this->abnormalCarRepository->create($input);

        return $this->sendResponse($abnormalCars->toArray(), '保存成功');
    }

    /**
     * Display the specified AbnormalCar.
     * GET|HEAD /abnormalCars/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var AbnormalCar $abnormalCar */
        $abnormalCar = $this->abnormalCarRepository->findWithoutFail($id);

        if (empty($abnormalCar)) {
            return $this->sendError('没有数据');
        }

        return $this->sendResponse($abnormalCar->toArray(), '查询成功');
    }

    /**
     * Update the specified AbnormalCar in storage.
     * PUT/PATCH /abnormalCars/{id}
     *
     * @param  int $id
     * @param UpdateAbnormalCarAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateAbnormalCarAPIRequest $request)
    {
        $input = $request->all();

        /** @var AbnormalCar $abnormalCar */
        $abnormalCar = $this->abnormalCarRepository->findWithoutFail($id);

        if (empty($abnormalCar)) {
            return $this->sendError('操作失败');
        }

        $abnormalCar = $this->abnormalCarRepository->update($input, $id);

        return $this->sendResponse($abnormalCar->toArray(), '操作成功');
    }

    /**
     * Remove the specified AbnormalCar from storage.
     * DELETE /abnormalCars/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var AbnormalCar $abnormalCar */
        $abnormalCar = $this->abnormalCarRepository->findWithoutFail($id);

        if (empty($abnormalCar)) {
            return $this->sendError('操作失败');
        }

        $abnormalCar->delete();

        return $this->sendResponse($id, '操作成功');
    }
}
